package report

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
	"xkginweb/commons/response"
	"xkginweb/global"
	basisRequest "xkginweb/model/entity/basis/request"
	resp "xkginweb/model/entity/comms/response"
	poolrequest "xkginweb/model/entity/coolingPool/request"
	pooldevicerequest "xkginweb/model/entity/coolingPoolDevice/request"
	devicerequest "xkginweb/model/entity/importantDevice/request"
	portrequest "xkginweb/model/entity/importantPort/request"
	"xkginweb/model/entity/report"
	"xkginweb/model/entity/report/request"
	"xkginweb/model/entity/room/vo"
)

// 定义api接口
type ReportApi struct {
	global.BaseApi
}

func (api *ReportApi) CreateReport(c *gin.Context) {
	// 1: 第一件事情就准备数据的载体
	var report report.QccReport
	err := c.ShouldBindJSON(&report)
	if err != nil {
		// 如果参数注入失败或者出错就返回接口调用这。出错了.
		response.FailWithMessage(err.Error(), c)
		return
	}
	// 从jwt中获取ReporterId
	value, _ := c.Get("ReporterId")

	// 创建实例，保存房间
	err = ReportService.CreateReport(&report)
	// 如果保存失败。就返回创建失败的提升
	if err != nil {
		response.FailWithMessage("创建失败", c)
		return
	}

	report.ReporterId = api.StringToUnit(fmt.Sprintf("%v", value))
	// 如果保存成功，就返回创建创建成功
	response.Ok("创建成功", c)
}

// 根据ID查询上报明细

func (api *ReportApi) GetReport(c *gin.Context) {
	// 绑定参数用来获取/:id这个方式
	id := c.Param("id")
	// 开始执行
	parseUint, _ := strconv.ParseUint(id, 10, 64)

	data, err := ReportService.GetReport(uint(parseUint))
	if err != nil {
		response.FailWithMessage("获取失败", c)
		return
	}

	response.Ok(data, c)
}

// 修改巡检记录

func (api *ReportApi) UpdateReport(c *gin.Context) {
	// 1: 第一件事情就准备数据的载体
	var report report.QccReport
	err := c.ShouldBindJSON(&report)
	if err != nil {
		// 如果参数注入失败或者出错就返回接口调用这。出错了.
		response.FailWithMessage(err.Error(), c)
		return
	}

	err = ReportService.UpdateReport(&report)
	// 如果保存失败。就返回创建失败的提升
	if err != nil {
		response.FailWithMessage("更新失败", c)
		return
	}
	// 如果保存成功，就返回创建创建成功
	response.Ok("更新成功", c)
}

func (api *ReportApi) LoadReportPage(c *gin.Context) {
	// 创建一个分页对象
	var pageInfo request.ReportPageInfo
	// 把前端json的参数传入给PageInfo
	err := c.ShouldBindJSON(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}

	coolingPoolDevicePage, total, err := ReportService.LoadReportPage(pageInfo)
	if err != nil {
		response.FailWithMessage("获取失败"+err.Error(), c)
		return
	}
	response.Ok(resp.PageResult{
		List:     coolingPoolDevicePage,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, c)
}

/**查询上报内容*/

func (api *ReportApi) GetReportByRoomId(c *gin.Context) {
	// 绑定参数用来获取/:id这个方式
	id := c.Param("id")

	roomId := api.StringToUnit(id)
	if roomId == 0 {
		response.FailWithMessage("请选择一个房间操作", c)
		return
	}
	var roomVo vo.RoomVo

	room, err := RoomService.GetRoom(roomId)
	if err != nil {
		response.FailWithMessage("查询房间失败", c)
		return
	}
	// 查询基础项
	var basisRequest basisRequest.BasisInfo
	basisRequest.RoomId = &roomId
	basis, err := BasisService.LoadBasis(basisRequest)
	if err != nil {
		response.FailWithMessage("查询基础项失败", c)
		return
	}
	// 查询重要设备
	var deviceRequest devicerequest.ImportantDeviceInfo
	deviceRequest.RoomId = &roomId
	device, err := ImportantDeviceService.LoadImportantDevice(deviceRequest)
	if err != nil {
		response.FailWithMessage("查询重要设备失败", c)
		return
	}
	// 查询重要端口
	var portRequest portrequest.ImportantPortInfo
	portRequest.RoomId = &roomId
	port, err := ImportantPortService.LoadImportantPort(portRequest)
	if err != nil {
		response.FailWithMessage("查询重要端口失败", c)
		return
	}
	// 查询冷池
	var poolRequest poolrequest.CoolingPoolInfo
	poolRequest.RoomId = &roomId
	pools, err := CoolingPoolService.LoadCoolingPool(poolRequest)
	if err != nil {
		response.FailWithMessage("查询冷池失败", c)
		return
	}
	// 查询冷池基础检查项
	var poolDeviceRequest pooldevicerequest.CoolingPoolDeviceInfo
	poolDeviceRequest.RoomId = &roomId
	pooldevice, err := CoolingPoolDeviceService.LoadCoolingPoolDevice(poolDeviceRequest)
	if err != nil {
		response.FailWithMessage("查询冷池基础检查项失败", c)
		return
	}
	// 处理冷池基础检查项
	// 将cooling_pool_device的值根据cooling_pool_id放入cooling_pool的children中
	for i, pool := range pools {
		for _, device := range pooldevice {
			if pool.CoolingPoolId == device.CoolingPoolId {
				pools[i].Children = append(pools[i].Children, device)
			}
		}
	}
	roomVo.RoomId = roomId
	roomVo.RoomName = room.RoomName
	roomVo.RoomType = room.RoomType
	roomVo.Basic = basis
	roomVo.ImportantPort = port
	roomVo.ImportantDevice = device
	roomVo.CoolingPool = pools
	//roomVo.CoolingPoolDevice = pooldevice
	response.Ok(roomVo, c)
}
